/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyClass;

import javax.microedition.lcdui.Display;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.midlet.*;

/**
 * @author HuuTai_it
 */
public class Control extends MIDlet {

    private Display d;
    public GameManagement g;
    public int life = 3;
    public int difficulties = 1;
    public int scores = 0;
    public int level = 1;
    public boolean activeSound = true;
    public Player bonusship = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/bonusship.wav"), "audio/x-wav");
    private Player explode = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/explode.wav"), "audio/x-wav");
    private Player monster = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/monster.wav"), "audio/x-wav");
    private Player move = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/move.wav"), "audio/x-wav");
    private Player select = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/select.wav"), "audio/x-wav");
    private Player shoot = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/shoot.wav"), "audio/x-wav");
    private Player updown = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/updown.wav"), "audio/x-wav");
    public Player welcome = Manager.createPlayer(getClass().getResourceAsStream("/MyAudio/welcome.wav"), "audio/x-wav");
    
    public Control() throws Exception {
        
        d = Display.getDisplay(this);
        bonusship.prefetch();
        explode.prefetch();
        monster.prefetch();
        move.prefetch();
        select.prefetch();
        shoot.prefetch();
        updown.prefetch();
        welcome.prefetch();
    }
    
    public void bonusship() {
        
        if(activeSound)
            try {

                bonusship.start();
            }
            catch (Exception ex) {}
    }
    
    public void explode() {
        
        if(activeSound)
            try {

                explode.start();
            }
            catch (Exception ex) {}
    }
    
    public void monster() {
        
        if(activeSound)
            try {

                monster.start();
            }
            catch (Exception ex) {}
    }
    
    public void move() {
        
        if(activeSound)
            try {

                move.start();
            }
            catch (Exception ex) {}
    }
    
    public void select() {
        
        if(activeSound)
            try {

                select.start();
            }
            catch (Exception ex) {}
    }
    
    public void shoot() {
        
        if(activeSound)
            try {

                shoot.start();
            }
            catch (Exception ex) {}
    }
    
    public void updown() {
        
        if(activeSound)
            try {

                updown.start();
            }
            catch (Exception ex) {}
    }
    
    public void welcome() {
        
        if(activeSound)
            try {

                welcome.start();
            }
            catch (Exception ex) {}
    }
    
    public void startApp() {
        
        showMainMenu();
    }
    
    public void pauseApp() {
    }   
    public void destroyApp(boolean unconditional) { 
        
        cleanUp();
        
        System.gc();
        notifyDestroyed();
    }   
    
    public void cleanUp() {
        
        if (bonusship != null) {
            
            bonusship.close();
            bonusship = null;
        }
        
        if (explode != null) {
            
            explode.close();
            explode = null;
        }
        
        if (monster != null) {
            
            monster.close();
            monster = null;
        }
        
        if (move != null) {
            
            move.close();
            move = null;
        }
        
        if (updown != null) {
            
            updown.close();
            updown = null;
        }
        
        if (select != null) {
            
            select.close();
            select = null;
        }
        
        if (shoot != null) {
            
            shoot.close();
            shoot = null;
        }
        
        if (welcome != null) {
            
            welcome.close();
            welcome = null;
        }
  }
    
    public void showMainMenu() {
        
        try {
            
            MainMenu mainMenu = new MainMenu(this);
            d.setCurrent(mainMenu);
        } 
        catch (Exception ex) {}     
    }
    
    public void showAboutScreen() {
        
        try {
            
            About about = new About(this);
            about.start();
            d.setCurrent(about);
        } 
        catch (Exception ex) {}
    }
    
    public void showHelpScreen() {
        
        try {
            
            Help help = new Help(this);
            d.setCurrent(help);
        } 
        catch (Exception ex) {}   
    }
    
    public void showSettingScreen() {
        
        try {
            
            Setting setting = new Setting(this);
            d.setCurrent(setting);
        } 
        catch (Exception ex) {}        
    }
    
    public void showHighScoresScreen() {
        
        try {
            HighScores highScores = new HighScores(this);
            d.setCurrent(highScores);
        } 
        catch (Exception ex) {}   
    }
    
    public void showLoadingScreen() {
        
        try {
            
            LoadingGame loadingGame = new LoadingGame(this);
            loadingGame.start();
            d.setCurrent(loadingGame);
        } 
        catch (Exception ex) {}
    }
    
    public void showGameOverScreen() {
        
        try {
            
            GameOver gameOver = new GameOver(g);
            d.setCurrent(gameOver);
        } 
        catch (Exception ex) {}
    }
    
    public void showGameWinScreen() {
        
        try {
            
            GameWin gameWin = new GameWin(g);
            d.setCurrent(gameWin);
        } 
        catch (Exception ex) {}
    }
    
    public void newGame() {        
        try {
            
            g = new GameManagement(this);
            g.start();
            d.setCurrent(g);
        } 
        catch (Exception ex) {}
    }  
    
    public void exitGame() {        
            destroyApp(true);
    }
}
